﻿using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastGPT_Admin.Service
{
    internal class TeamsService
    {
        private readonly IMongoCollection<Teams> _collection;

        public TeamsService(string connectionString, string databaseName, string collectionName)
        {
            // 创建MongoClient
            var client = new MongoClient(connectionString);
            // 获取数据库
            IMongoDatabase database = client.GetDatabase(databaseName);
            // 获取集合
            _collection = database.GetCollection<Teams>(collectionName);
        }

        // 创建（Create）
        public ObjectId Create(Teams model)
        {
            _collection.InsertOne(model);
            return model.Id;
        }

        // 读取（Read）
        public List<Teams> Read(string nameValue = "", bool vague = true)
        {
            if (String.IsNullOrEmpty(nameValue))
            {
                var filter = _collection.Find(new BsonDocument()).ToList();
                return filter;
            } else
            {
                if (vague) {
                    // var filter = Builders<Teams>.Filter.Eq(m => m.Name, nameValue);
                    var filter = Builders<Teams>.Filter.Regex(m => m.Name, new BsonRegularExpression($".*{nameValue}.*", "i"));
                    return _collection.Find(filter).ToList();
                } else
                {
                    var filter = Builders<Teams>.Filter.Eq(m => m.Name, nameValue);
                    return _collection.Find(filter).ToList();
                }
            }
        }

        public List<Teams> ReadByID(string Id)
        {
            // 将字符串ID转换为ObjectId
            ObjectId objectId = new ObjectId(Id);
            // 构建过滤器，确保类型匹配
            var filter = Builders<Teams>.Filter.Eq(m => m.Id, objectId);
            // 查找并返回结果
            return _collection.Find(filter).ToList();
        }

        // 更新（Update）
        public void Update(string property1Value, Teams updatedModel)
        {
            // var filter = Builders<Teams>.Filter.Eq(m => m.Property1, property1Value);
            // var update = Builders<Teams>.Update.Set(m => m.Property2, updatedModel.Property2);
            // 根据需要可以添加更多更新操作
            // _collection.UpdateOne(filter, update);
        }

        // 删除（Delete）
        public void Delete(string property1Value)
        {
            // var filter = Builders<Teams>.Filter.Eq(m => m.Property1, property1Value);
            // _collection.DeleteOne(filter);
        }
    }

    public class Teams
    {
        [BsonId]
        public ObjectId Id { get; set; }

        [BsonElement("name")]
        public string? Name { get; set; }

        [BsonElement("ownerId")]
        public ObjectId OwnerId { get; set; }

        [BsonElement("avatar")]
        public string? Avatar { get; set; }

        [BsonElement("createTime")]
        public DateTime CreateTime { get; set; }

        [BsonElement("defaultPermission")]
        public int DefaultPermission { get; set; }

        [BsonElement("balance")]
        public int Balance { get; set; }

        [BsonElement("__v")]
        public int Version { get; set; }
    }
}
